package cn.lym.zxxx.session;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

public abstract class ShareHttpSession implements HttpSession {

    /**
     * 是否是新建的session
     */
    private boolean isNew = true;
    /**
     * SessionId
     */
    private String sessionId;
    /**
     * session管理器
     */
    private ShareSessionManager shareSessionManager;
    /**
     * 中间件的session对象
     */
    private HttpSession httpSession;

    private long createTime;

    public ShareHttpSession(String sessionId, ShareSessionManager shareSessionManager, HttpSession httpSession, long createTime, boolean isNew) {
        this.sessionId = sessionId;
        this.shareSessionManager = shareSessionManager;
        this.httpSession = httpSession;
        this.createTime = createTime;
        this.isNew = isNew;
    }

    @Override
    public long getCreationTime() {
        return this.createTime;
    }

    @Override
    public String getId() {
        return this.sessionId;
    }

    @Override
    public ServletContext getServletContext() {
        return this.getHttpSession().getServletContext();
    }

    @Override
    public void setMaxInactiveInterval(int interval) {
        this.getHttpSession().setMaxInactiveInterval(interval);
    }

    @Override
    public int getMaxInactiveInterval() {
        return this.getHttpSession().getMaxInactiveInterval();
    }

    @Deprecated
    @Override
    public HttpSessionContext getSessionContext() {
        return null;
    }

    @Override
    public Object getValue(String name) {
        return this.getAttribute(name);
    }

    @Override
    public Enumeration<String> getAttributeNames() {
        return Collections.enumeration(this.getShareAttributeNames());
    }

    @Override
    public String[] getValueNames() {
        return this.getShareAttributeNames().toArray(new String[0]);
    }

    @Override
    public void putValue(String name, Object value) {
        this.setAttribute(name, value);
    }

    @Override
    public void removeValue(String name) {
        this.removeAttribute(name);
    }

    @Override
    public boolean isNew() {
        return this.isNew;
    }

    abstract public List<String> getShareAttributeNames();

    public ShareSessionManager getShareSessionManager() {
        return shareSessionManager;
    }

    public void setShareSessionManager(ShareSessionManager shareSessionManager) {
        this.shareSessionManager = shareSessionManager;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public void setHttpSession(HttpSession httpSession) {
        this.httpSession = httpSession;
    }

    public HttpSession getHttpSession() {
        return httpSession;
    }

    public void setIsNew(boolean isNew) {
        this.isNew = isNew;
    }
}
